package com.hxexp.weixin.mvc.refund.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hxexp.weixin.common.cache.PassengerTicketCacheManager;
import com.hxexp.weixin.common.cache.model.PassengerTicketInfo;
import com.hxexp.weixin.common.exception.APIException;
import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.hpe.api.refund.RefundAPI;
import com.hxexp.weixin.hpe.api.refund.model.RefundParamBean;
import com.hxexp.weixin.hpe.api.refund.model.RefundResultBean;
import com.hxexp.weixin.hpe.api.returnamount.ReturnAmountAPI;
import com.hxexp.weixin.hpe.api.returnamount.model.ReturnAmountParamBean;
import com.hxexp.weixin.hpe.api.returnamount.model.ReturnAmountResultBean;
import com.hxexp.weixin.mvc.refund.model.RefundInfoBean;
import com.hxexp.weixin.mvc.refund.model.TicketInfoBean;
import com.hxexp.weixin.mvc.refund.service.RefundService;

import net.sf.cglib.beans.BeanCopier;

/**
 * 退票管理实现类
 *
 * @author wang.bin
 * @author li.jiang
 */
@Service
public final class RefundServiceImpl implements RefundService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RefundServiceImpl.class);
    private final BeanCopier beanCopier = BeanCopier.create(TicketInfoBean.class, PassengerTicketInfo.class, false);

    @Autowired
    private ReturnAmountAPI returnAmountAPI;
    @Autowired
    private RefundAPI refundAPI;
    @Autowired
    PassengerTicketCacheManager pticketCacheManager;

    @Override
    public ReturnAmountResultBean getReturnAmount(List<TicketInfoBean> ticketInfoList) throws ServiceException {

        ReturnAmountResultBean returnAmountResult = new ReturnAmountResultBean();
        BigDecimal totalAmount = new BigDecimal("0.00");

        try {
            for (TicketInfoBean ticketInfo : ticketInfoList) {
                ReturnAmountParamBean returnAmoutParam = new ReturnAmountParamBean();
                returnAmoutParam.setTicketId(ticketInfo.getTicketId());

                returnAmountResult = returnAmountAPI.getReturnAmount(returnAmoutParam);

                BigDecimal returnAmount = new BigDecimal(returnAmountResult.getReturnAmount());
                BigDecimal amount = totalAmount.add(returnAmount);
                totalAmount = amount;
            }

            double total = totalAmount.doubleValue();
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");
            returnAmountResult.setTotalReturnAmount(df.format(total));
        } catch (APIException e) {
            throw new ServiceException(e);
        }

        return returnAmountResult;
    }

    @Override
    public List<RefundInfoBean> refund(List<TicketInfoBean> checkedTicketInfoList, String orderId, String stationId)
            throws ServiceException {
        RefundResultBean refundResult = new RefundResultBean();
        List<RefundInfoBean> refundInfoList = new ArrayList<RefundInfoBean>();

        try {
            for (TicketInfoBean ticketInfo : checkedTicketInfoList) {
                RefundParamBean refundParam = new RefundParamBean();
                refundParam.setOrderId(orderId);
                refundParam.setStationId(stationId);
                refundParam.setTicketId(ticketInfo.getTicketId());

                refundResult = refundAPI.refund(refundParam);

                // 封装返回结果LIST
                RefundInfoBean refundInfo = new RefundInfoBean();
                refundInfo.setErrcode(refundResult.getErrcode());
                refundInfo.setErrmsg(refundResult.getErrmsg());
                refundInfo.setTicketId(ticketInfo.getTicketId());
                refundInfo.setFullname(ticketInfo.getFullname());
                refundInfoList.add(refundInfo);
                LOGGER.debug("***********退票返回参数：{}", refundInfo);

                deletePassengerTicketInfo(ticketInfo);
            }
        } catch (APIException e) {
            throw new ServiceException(e);
        }
        return refundInfoList;
    }

    /**
     * 在缓存中删除对应的乘客购票信息
     *
     * @param ticketInfo
     *            车票信息
     */
    private void deletePassengerTicketInfo(TicketInfoBean ticketInfo) {
        PassengerTicketInfo passengerTicketInfo = new PassengerTicketInfo();
        beanCopier.copy(ticketInfo, passengerTicketInfo, null);

        String key = pticketCacheManager.generateKey(passengerTicketInfo.toString());
        // 删除乘客购票记录
        pticketCacheManager.evict(key);
    }
}
